﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConquerServer_Basic
{
    public interface IClassPacket
    {
        void Deserialize(byte[] Bytes);
        byte[] Serialize();
    }

    public class PacketBuilder
    {
        public static byte[] AuthResponse(string ServerIP, uint Key1, uint Key2, ushort Port)
        {
            byte[] Packet = new byte[33];
            WriteUInt16(32, Packet, 0);
            WriteUInt16(1055, Packet, 2);
            WriteUInt32(Key2, Packet, 4);
            WriteUInt32(Key1, Packet, 8);
            WriteString(ServerIP, Packet, 12);
            WriteUInt16(Port, Packet, 28);
            return Packet;
        }
        public static byte[] CharacterInfo(GameClient Client)
        {
            byte[] Packet = new byte[((70 + Client.Spouse.Length) + Client.Entity.Name.Length) + 1];
            WriteUInt16((ushort)Packet.Length, Packet, 0);
            WriteUInt16(1006, Packet, 2);
            WriteUInt32(Client.Entity.UID, Packet, 4);
            WriteUInt32(Client.Entity.Model, Packet, 8);
            WriteUInt16(Client.Entity.HairStyle, Packet, 12);
            WriteUInt32((uint)Client.Money, Packet, 14);
            WriteUInt32((uint)Client.ConquerPoints, Packet, 18);
            WriteUInt16(Client.Strength, Packet, 46);
            WriteUInt16(Client.Agility, Packet, 48);
            WriteUInt16(Client.Vitality, Packet, 50);
            WriteUInt16(Client.Spirit, Packet, 52);
            WriteUInt16(Client.StatPoints, Packet, 54);
            WriteUInt16((ushort)Client.Entity.Hitpoints, Packet, 56);
            WriteUInt16(Client.Mana, Packet, 58);
            Packet[62] = Client.Entity.Level;
            Packet[63] = Client.Job;
            Packet[64] = 5;
            Packet[65] = (byte)Client.Entity.Reborn;
            Packet[66] = 1;
            Packet[67] = 2;
            WriteStringWithLength(Client.Entity.Name, Packet, 68); 
            WriteStringWithLength(Client.Spouse, Packet, (ushort)(69 + Packet[68]));
            return Packet;
        }

        public static void ZeroFill(byte[] Buffer, ushort Offset, ushort Count)
        {
            for (ushort i = 0; i < Count; i++)
                Buffer[i + Offset] = 0x00;
        }
        public static void WriteStringWithLength(string Arg, byte[] Buffer, ushort Offset)
        {
            Buffer[Offset] = (byte)Arg.Length;
            Offset++;
            ushort i = 0;
            while (i < Arg.Length)
            {
                Buffer[(ushort)(i + Offset)] = (byte)Arg[i];
                i = (ushort)(i + 1);
            }
        }
        public static void WriteString(string Arg, byte[] Buffer, ushort Offset)
        {
            ushort i = 0;
            while (i < Arg.Length)
            {
                Buffer[(ushort)(i + Offset)] = (byte)Arg[i];
                i = (ushort)(i + 1);
            }
        }
        public static void WriteUInt16(ushort Arg, byte[] Buffer, ushort Offset)
        {
            Buffer[Offset] = (byte)(Arg);
            Buffer[Offset + 1] = (byte)(Arg >> 8);
        }
        public static void WriteUInt32(uint Arg, byte[] Buffer, ushort Offset)
        {
            Buffer[Offset] = (byte)(Arg);
            Buffer[Offset + 1] = (byte)(Arg >> 8);
            Buffer[Offset + 2] = (byte)(Arg >> 16);
            Buffer[Offset + 3] = (byte)(Arg >> 24);
        }
    }
}
